package org.study.swing.study01.example;

import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

/**
 * 参考 https://blog.csdn.net/m0_61495539/article/details/134809156
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月11日
 */
@Slf4j
public class LoginFrame extends JFrame {
    public static void main(String[] args) throws AWTException {
        new LoginFrame();
    }

    // 设置第二个参数，使之居中显示
    private JLabel topNameLabel = new JLabel("学生信息管理系统", JLabel.CENTER);
    private SpringLayout springLayout = new SpringLayout();
    // centerPanel使用弹簧布局
    private JPanel centerPanel = new JPanel(springLayout);
    private JLabel usernameLabel = new JLabel("账号：");
    private JTextField usernameTextField = new JTextField();
    private JLabel passwordLabel = new JLabel("密码：");
    private JPasswordField passwordTextField = new JPasswordField();
    private JButton loginButton = new JButton("登录");
    private JButton resetButton = new JButton("重置");

    public LoginFrame() throws AWTException {
        // 设置标题
        super("学生信息管理系统登录");

        // 获取内容面板
        Container contentPane = super.getContentPane();

        // 自定义图标
        URL url = LoginFrame.class.getClassLoader().getResource("photo.png");
        Image image = new ImageIcon(url).getImage();
        super.setIconImage(image);

        // 设置字体
        Font font = new Font("宋体", Font.PLAIN, 40);
        topNameLabel.setFont(font);
        topNameLabel.setPreferredSize(new Dimension(0, 80));

        Font font1 = new Font("楷体", Font.PLAIN, 20);
        usernameLabel.setFont(font1);
        // 设置文本框大小
        usernameTextField.setPreferredSize(new Dimension(200, 30));
        passwordLabel.setFont(font1);
        passwordTextField.setPreferredSize(new Dimension(200, 30));
        // 使用*代替输入的字符，默认是圆点
        passwordTextField.setEchoChar('*');
        loginButton.setFont(font1);
        loginButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameTextField.getText();
                String password = passwordTextField.getText();
                log.info("username={}，password={}", username, password);
            }
        });
        resetButton.setFont(font1);
        resetButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                usernameTextField.setText("");
                passwordTextField.setText("");
            }
        });

        // 将所有组件都加入到中间面板中：centerPanel
        centerPanel.add(usernameLabel);
        centerPanel.add(usernameTextField);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordTextField);
        centerPanel.add(loginButton);
        centerPanel.add(resetButton);

        // 将标题加入到面板的北部
        contentPane.add(topNameLabel, BorderLayout.NORTH);
        // 将中心面板加入到内容面板中
        contentPane.add(centerPanel, BorderLayout.CENTER);

        // 设置中心面板中内容的弹簧布局
        // 1、布局 usernameLabel  (是下方组件的参照物)
        Spring userNameLabelWidth = Spring.width(usernameLabel);
        Spring userNameTextFieldWidth = Spring.width(usernameTextField);
        // 左右间距
        Spring springWidth = Spring.constant(20);
        Spring childWidth = Spring.sum(Spring.sum(userNameLabelWidth, userNameTextFieldWidth), springWidth);
        // 得到水平偏移量
        int offsetX = childWidth.getValue() / 2;
        springLayout.putConstraint(SpringLayout.WEST, usernameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, usernameLabel, 20, SpringLayout.NORTH, centerPanel);
        // 2、userNameTextField
        springLayout.putConstraint(SpringLayout.WEST, usernameTextField, 5, SpringLayout.EAST, usernameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, usernameTextField, 0, SpringLayout.NORTH, usernameLabel);
        // 3、passwordLabel
        springLayout.putConstraint(SpringLayout.EAST, passwordLabel, 0, SpringLayout.EAST, usernameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, passwordLabel, 20, SpringLayout.SOUTH, usernameLabel);
        // 4、passWordTextField
        springLayout.putConstraint(SpringLayout.WEST, passwordTextField, 5, SpringLayout.EAST, passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH, passwordTextField, 0, SpringLayout.NORTH, passwordLabel);
        // 5、loginButton
        springLayout.putConstraint(SpringLayout.WEST, loginButton, 40, SpringLayout.WEST, passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginButton, 40, SpringLayout.SOUTH, passwordLabel);
        // 6、resetButton
        springLayout.putConstraint(SpringLayout.WEST, resetButton, 60, SpringLayout.EAST, loginButton);
        springLayout.putConstraint(SpringLayout.NORTH, resetButton, 0, SpringLayout.NORTH, loginButton);
        super.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        super.setLocationRelativeTo(null);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
        super.setResizable(false);
        super.setVisible(true);

        // 托盘
        // 首先判断系统是否支持
        if (SystemTray.isSupported()) {
            // 系统托盘
            SystemTray systemTray = SystemTray.getSystemTray();
            URL imgUrl = LoginFrame.class.getClassLoader().getResource("photo.png");
            // 自定义一个托盘
            TrayIcon trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
            // 将自定义的托盘加入到系统托盘中
            systemTray.add(trayIcon);
            // 设置托盘图片大小自动缩放， 一定要设置，不然图标可能显示不出来
            trayIcon.setImageAutoSize(true);
            // 最小化的时候销毁窗口
            super.addWindowListener(new WindowAdapter() {
                @Override
                public void windowIconified(WindowEvent e) {
                    LoginFrame.this.dispose();
                }
            });
            // 鼠标点击一次，让窗口正常显示
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        LoginFrame.this.setExtendedState(JFrame.NORMAL);
                    }
                    LoginFrame.this.setVisible(true);
                }
            });
        }
    }
}
